﻿using JHService;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Model;
using Model.Configuration_Model;
using Model.Entities_EF;
using Mqtt.Callback.Handlers;
using OperationDataBase;
using Service.Mqtt;
using Service.Mqtt.Callback;
using Service.Mqtt.Callback.Handlers;
using Service.Socket;
using System;
using System.Runtime.InteropServices;

namespace TcpService
{
    /// <summary>
    /// 用于存储 IServiceProvider 的数据
    /// </summary>
    public class ServiceLocator
    {
        public static IServiceProvider Instance { get; set; }
    }
    public class ReadServices
    {
        /// <summary>
        /// Socket 相关的注入
        /// </summary>
        /// <param name="services"></param>
        public static void ReadSocket(IServiceCollection services)
        {
            //Socket 服务
            services.AddSingleton<Services>();
            services.AddSingleton<Socket_RSServer>();


            //services.AddScoped<ReceiveParamHelp>();
            services.AddScoped<ParamIdsDataHelp>();
            services.AddScoped<WriteParamHelp>();
        }
        /// <summary>
        /// Http 相关的注入
        /// </summary>
        /// <param name="services"></param>
        public static void ReadHttp(IServiceCollection services)
        {
            //http 服务
            services.AddSingleton<HttpProgram>();
        }
        
        public static void ReadSqlserver(IServiceCollection services)
        {
            services.AddSingleton<Data_Storage>();
            
            services.AddSingleton<Tbhistory>();

            //services.AddSingleton<SqlContext>();

            //services.AddSingleton<ICPrkmonitorContext>();
        }
        /// <summary>
        /// MQTT相关的
        /// </summary>
        /// <param name="services"></param>
        public static void ReadMqtt(IServiceCollection services)
        {
            //MQTT 服务
            services.AddSingleton<CustomMqttFactory>();
            services.AddSingleton<MqttServerService>();
            services.AddSingleton<MqttServerStorage>();


            services.AddSingleton<MqttClientConnectedHandler>();
            services.AddSingleton<MqttClientDisconnectedHandler>();
            services.AddSingleton<MqttClientSubscribedTopicHandler>();
            services.AddSingleton<MqttClientUnsubscribedTopicHandler>();
            services.AddSingleton<MqttServerConnectionValidator>();
            services.AddSingleton<MqttApplicationMessageReceivedHandler>();
            services.AddSingleton<MqttApplicationMessageInterceptor>();
            services.AddSingleton<MqttServerSubscriptionInterceptor>();
        }
        public static void ReadSettings(IConfiguration configuration, IServiceCollection services)
        {
            var rsServerSettingsModel = new RSServerSettingsModel();
            configuration.Bind("RSServerSettings", rsServerSettingsModel);
            services.AddSingleton(rsServerSettingsModel);

            var mqttSettings = new MqttSettingsModel();
            configuration.Bind("MQTT", mqttSettings);
            services.AddSingleton(mqttSettings);
        }
    }


    public class ReadLogging
    {
        public static void Logging(ILoggingBuilder logging)
        {
            logging.AddConsole();
            var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
            if (isWindows)
            {
                // Add the EventLogLoggerProvider on windows machines
                logging.AddEventLog();
            }
            logging.AddFilter("Microsoft", LogLevel.Error);
            logging.AddFilter("System", LogLevel.Error);
        }
    }
}
